﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Southridge.View;
using System.Windows.Input;
using System.Windows;

namespace Southridge.View
{
    public class Document
    {
        protected Document ()
        {
        }

        static protected ICommand _newProfile = new DelegateCommand<object>(OnNewProfile, false);
        static protected ICommand _saveProfile = new DelegateCommand<object>(OnSaveProfile, CanSaveProfile, false);
        static protected ICommand _openProfile = new DelegateCommand<object>(OnOpenProfile, false); 

        public static ICommand NewProfile 
        {
            get
            {
                return _newProfile; 
            } 
        }

        public static ICommand SaveProfile 
        {
            get
            {
                return _saveProfile; 
            } 
        }

        public static ICommand OpenProfile
        {
            get
            {
                return _openProfile;
            }
        }

        public static ICommand ShowSearch
        {
            get
            {
                return new DelegateCommand<object> ( OnShowSearch , false ); 
            } 
        } 

        public static Document Create () 
        { 
            return new Document (); 
        } 

        private static Document _instance; 
        public static Document Current 
        { 
          get 
            { 
                if ( _instance == null ) 
                { 
                    _instance =  Document.Create () ; 
                } 
                    return _instance; 
            }
        }

        public bool IsDirty
        {
            get
            {
                return false;
            }
        }

        protected static void OnShowSearch(object o)
        {
            bool OKToSwitch = true; 
            if (OKToSwitch)
            {
                ViewManager.Current.Transition(ViewManager.Current.GetView ( Views.Search )) ; 
            }

        } 

        protected static void OnSaveProfile(object o)
        {
             
        }

        protected static void OnOpenProfile(object o)
        {
            MessageBox.Show("Open profile not implemented yet" + o.ToString() ); 
        }

        protected static bool CanSaveProfile(object o)
        {
            return Document.Current.IsDirty; 
        }
         

        protected static void OnNewProfile(object o)
        {
            bool OKToSwitch = !Document.Current.IsDirty; 

            if (!OKToSwitch)
            {
                MessageBoxResult res = MessageBox.Show( "Save changes to current profile" , "Caption" , MessageBoxButton.YesNoCancel);

                if (res == MessageBoxResult.Cancel)
                    return;

                OKToSwitch = true;  
            }

            if (OKToSwitch)
            {
                FrameworkElement fe = ViewManager.Current.GetView(Views.Profile);
                if (fe != null)
                {

                    ViewManager.Current.Transition( fe );
                } 
            } 
        }          
    }
}
